package com.base.tool.ui;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.base.R;

import java.util.ArrayList;


public class ActivityLauncher extends ListActivity {
    private String TAG = "ActivityLauncher";

    public ArrayList<ActivityInfo> mActivities = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.launcher);

        initData();


        setListAdapter(new MyAdapter());
    }

    public void initData() {
        //获取本应用的所有activity，排除本activity
        mActivities = new ArrayList<>();
        Tool.getActivity(this, mActivities);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent();
        intent.setClassName(this, mActivities.get(position).name);
        startActivity(intent);
    }

    private class MyAdapter extends ArrayAdapter<ActivityInfo> {
        MyAdapter() {
            super(ActivityLauncher.this, R.layout.launcher_item, R.id.activity_title, mActivities);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);

            TextView title = v.findViewById(R.id.activity_title);
            TextView desc = v.findViewById(R.id.activity_desc);

            CharSequence titleText = mActivities.get(position).nonLocalizedLabel;
            title.setText(String.format("[%d] ", position + 1) + (TextUtils.isEmpty(titleText) ? "－－" : titleText));

            desc.setText("className :" + mActivities.get(position).name + "\n" +
//                    mActivities.get(position).nonLocalizedLabel + "\n" +
                            "taskAffinity :" + mActivities.get(position).taskAffinity + "\n" +
                            "launchMode:" + Tool.getLaunchMode(mActivities.get(position).launchMode)
            );


            return v;
        }
    }

}
